﻿using Bll;
using DecorationSys.Models;
using Entity;
using Entity.DTO;
using IBll;
using Microsoft.AspNetCore.Mvc;

namespace DecorationSys.Controllers
{
    /// <summary>
    /// 订单相关
    /// </summary>
    public class OrderController : Controller
    {
        IUserInfoBll _userInfoBll;
        IOrderBll _orderBll;

        public OrderController(IOrderBll orderBll, IUserInfoBll userInfoBll)
        {
            _orderBll = orderBll;
            _userInfoBll = userInfoBll;
        }

        /// <summary>
        /// 订单列表视图
        /// </summary>
        /// <returns></returns>
        public IActionResult OrderListView()
        {
            return View();
        }

        /// <summary>
        /// 添加订单视图
        /// </summary>
        /// <returns></returns>
        public IActionResult CreateOrderView()
        {
            return View();
        }

        /// <summary>
        /// 修改订单视图
        /// </summary>
        /// <returns></returns>
        public IActionResult UpdateOrderView()
        {
            return View();
        }

        /// <summary>
        /// 订单详情视图
        /// </summary>
        /// <returns></returns>
        public IActionResult DetailsView()
        {
            return View();
        }

        /// <summary>
        /// 打印账单视图
        /// </summary>
        /// <returns></returns>
        public IActionResult PrintOrder()
        {
            return View();
        }

        /// <summary>
        /// 获取打印订单数据打印账单
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public MyJsonResult GetPrintOrderData(string orderId)
        {
            if (orderId == null)
            {
                return MyJsonResult.GetErrorResult("订单不存在");
            }

            var data = _orderBll.GetPrintOrderData(orderId);

            if (data == null)
            {
                return MyJsonResult.GetErrorResult("数据不存在");
            }
            else
            {
                return MyJsonResult.GetSuccessResult(data);
            }
        }

        /// <summary>
        /// 根据订单id获取订单信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public MyJsonResult GetOrderById(string id)
        {
            var order = _orderBll.GetOrderById(id);

            if (order == null)
            {
                return MyJsonResult.GetErrorResult("订单不存在");
            }
            else
            {
                return MyJsonResult.GetSuccessResult(order);
            }
        }

        /// <summary>
        /// 获取更新订单页面信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public MyJsonResult GetUpdateOrderDatas(string id)
        {
            var order = _orderBll.GetOrderById(id);

            var options = _userInfoBll.GetSelectOptionList();

            if (order == null)
            {
                return MyJsonResult.GetErrorResult("订单不存在");
            }
            else
            {
                return MyJsonResult.GetSuccessResult(new 
                {
                    order,
                    options
                });
            }
        }

        /// <summary>
        /// 更新订单
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public MyJsonResult UpdateOrder(string id, string customerName, string customerPhone, string customerAddress, double area, string agentName, string agentPhone, DateTime startTime, DateTime endTime, string designerId)
        {
            //参数验证
            if (string.IsNullOrEmpty(id))
            {
                return MyJsonResult.GetErrorResult("订单id不能为空");
            }
            if (string.IsNullOrEmpty(customerName))
            {
                return MyJsonResult.GetErrorResult("请输入客户姓名");
            }
            if (string.IsNullOrEmpty(customerPhone))
            {
                return MyJsonResult.GetErrorResult("请输入客户手机");
            }
            if (string.IsNullOrEmpty(customerAddress))
            {
                return MyJsonResult.GetErrorResult("请输入客户地址");
            }


            string msg;
            //调用业务逻辑层
            bool isSuccess = _orderBll.UpdateOrder( id,  customerName,  customerPhone, customerAddress,  area,  agentName,  agentPhone,  startTime,  endTime,  designerId, out msg);
            if (isSuccess)
            {
                return MyJsonResult.GetSuccessResult();
            }
            else
            {
                return MyJsonResult.GetErrorResult(msg);
            }
        }


        /// <summary>
        /// 获取订单列表数据
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public object GetOrderList(string title, int page, int limit)
        {
            if (page <= 0)
            {
                return new
                {
                    code = 0,
                    msg = "页码有误",
                    count = 0,
                    data = new List<object>()
                };
            }

            if (limit <= 0)
            {
                return new
                {
                    code = 0,
                    msg = "每页条数有误",
                    count = 0,
                    data = new List<object>()
                };
            }

            int count;

            //查订单数据
            var list = _orderBll.GetOrderList(title, page, limit, out count);

            return new
            {
                code = 0,
                msg = "cg",
                count,
                data = list
            };
        }

        /// <summary>
        /// 创建订单
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public MyJsonResult CreateOrder(string customerName, string customerPhone, string customerAddress, double area, string agentName, string agentPhone, DateTime startTime, DateTime endTime, string designerId)
        {
            //参数验证
            if (string.IsNullOrEmpty(customerName))
            {
                return MyJsonResult.GetErrorResult("请输入客户姓名");
            }
            if (string.IsNullOrEmpty(customerPhone))
            {
                return MyJsonResult.GetErrorResult("请输入客户手机");
            }
            if (string.IsNullOrEmpty(customerAddress))
            {
                return MyJsonResult.GetErrorResult("请输入客户地址");
            }

            string userId = HttpContext.Session.GetString("UserId");

            //用户id有值的时候才去数据库查菜单数据
            if (string.IsNullOrEmpty(userId))
            {
                return MyJsonResult.GetErrorResult("请重新登录");
            }



            string msg;
            bool isSuccess = _orderBll.CreateOrder( customerName,  customerPhone,  customerAddress,  area,  agentName,  agentPhone,  startTime,  endTime,  designerId, userId, out msg);

            if (isSuccess)
            {
                return MyJsonResult.GetSuccessResult();
            }
            else
            {
                return MyJsonResult.GetErrorResult(msg);
            }
        }

        /// <summary>
        /// 软删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost]
        public MyJsonResult DeleteOrder(string id)
        {
            if (string.IsNullOrEmpty(id))
            {
                return MyJsonResult.GetErrorResult("参数有误");
            }

            bool isSuccess = _orderBll.DeleteOrder(id);

            if (isSuccess)
            {
                return MyJsonResult.GetSuccessResult();
            }
            else
            {
                return MyJsonResult.GetErrorResult("失败");
            }
        }

        /// <summary>
        /// 批量软删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost]
        public MyJsonResult DeleteOrders(List<string> ids)
        {
            if (ids.Count <= 0 || ids == null)
            {
                return MyJsonResult.GetErrorResult("参数有误");
            }

            bool isSuccess = _orderBll.DeleteOrders(ids);

            if (isSuccess)
            {
                return MyJsonResult.GetSuccessResult();
            }
            else
            {
                return MyJsonResult.GetErrorResult("失败");
            }
        }

    }

}
